//
//  MSSegmentedButton.m
//  Community
//
//  Created by luo danal on 3/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MSSegmentedButton.h"
#import <QuartzCore/QuartzCore.h>

#define kMSSegmentedButtonLabelTag 10000
#define kMSSegmentedButtonButtonTag 20000

@implementation MSSegmentedButtonItem
@synthesize count = _count;
@synthesize title = _title;
@synthesize image = _image;
@synthesize imageHighlighted = _imageHighlighted;

- (void)dealloc{
    [_title release];
    [_image release];
    [_imageHighlighted release];
    [super dealloc];
}

- (id)initWithImage:(UIImage *)image imageHighlighted:(UIImage *)image2{
    self = [super init];
    if (self) {
        self.image = image;
        self.imageHighlighted = image2;
    }
    return self;
}

- (id)initWithTitle:(NSString *)title count:(NSInteger)count{
    self = [super init];
    if (self) {
        self.title = title;
        self.count = count;
        
    }
    return self;
}

@end



#pragma mark - 

@implementation MSSegmentedButton
@synthesize type = _type;
@synthesize items = _items;
@synthesize numberOfSegment = _numberOfSegment;
@synthesize delegate = _delegate;

- (void)dealloc{
    [_items release];
    [super dealloc];
}

- (void)initiate{
    CALayer *layer = self.layer;
    layer.cornerRadius = 5.f;
    layer.borderColor = [UIColor lightGrayColor].CGColor;
    layer.borderWidth = 1.f;
    layer.masksToBounds = YES;
    self.backgroundColor = [UIColor whiteColor];
}

- (CGFloat)margin{
    return 7.f;
}

//- (id)initWithFrame:(CGRect)frame type:(MSSegmentedType)type numberOfSegment:(NSInteger)count
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        // Initialization code
//        self.type = type;
//        self.numberOfSegment = count;
//        if (type == kMSSegmentedTypeImage) {
//            
//        } else {
//            
//        }
//    }
//    return self;
//}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(c, [UIColor grayColor].CGColor);
    float w = _unitWidth;
    float h = rect.size.height;
    for (int i = 0; i < self.numberOfSegment - 1; i++) {

        CGContextMoveToPoint(c, w, 0);
        CGContextAddLineToPoint(c, w, h);
        w += _unitWidth;
        
    }
    CGContextSetAllowsAntialiasing(c, 1);
    CGContextSetLineWidth(c, 1.f);
    CGContextStrokePath(c);
}


- (id)initWithFrame:(CGRect)frame titleItems:(NSArray *)items{
    self = [super initWithFrame:frame];
    if (self) {
        [self initiate];
        self.items = items;
        self.numberOfSegment = [items count];
        
        _unitWidth = frame.size.width/self.numberOfSegment;
        float margin = [self margin];
        float w = _unitWidth - 2*margin;
        float h = (frame.size.height - 2*margin)/2;
        CGRect rect = CGRectMake(margin, margin, _unitWidth - 2*margin, frame.size.height - 2*margin);
        
        rect = CGRectMake(margin, margin, w, h);
        MSSegmentedButtonItem *item = nil;
        for (int i = 0; i < [items count]; i++) {
            item = [items objectAtIndex:i];
            
            rect.origin.y = margin;
            rect.size.height = h;
            UILabel *label = [[UILabel alloc] initWithFrame:rect];
            label.textAlignment = UITextAlignmentCenter;
            label.textColor = [UIColor colorWithRed:0.f/255 green:153.f/255 blue:255.f/255 alpha:1.0];
            label.text = [NSString stringWithFormat:@"%d",item.count];
            label.tag = kMSSegmentedButtonLabelTag + i;
            [self addSubview:label];
            [label release];
            
            rect.origin.y = h + h/2;
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:rect];
            titleLabel.text = item.title;
            titleLabel.textAlignment = UITextAlignmentCenter;
            [self addSubview:titleLabel];
            [titleLabel release];
            
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.tag = kMSSegmentedButtonButtonTag + i;
            button.showsTouchWhenHighlighted = YES;
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            button.frame = CGRectMake(rect.origin.x, margin, w, 2*h);
            [self addSubview:button];
            
            rect.origin.x += _unitWidth;
        }
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame imageItems:(NSArray *)items{
    self = [super initWithFrame:frame];
    if (self) {
        [self initiate];
        self.items = items;
        self.numberOfSegment = [items count];
        
        _unitWidth = frame.size.width/self.numberOfSegment;
        float margin = [self margin];
        CGRect rect = CGRectMake(margin, margin, _unitWidth - 2*margin, frame.size.height - 2*margin);
        
//        for (MSSegmentedButtonItem *item in items) {
        MSSegmentedButtonItem *item = nil;
        for (int i = 0; i < [items count]; i++) {

            item = [items objectAtIndex:i];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.imageView.contentMode = UIViewContentModeScaleAspectFit;
            button.tag = kMSSegmentedButtonButtonTag + i;
            button.frame = rect;
            [button setImage:item.image forState:UIControlStateNormal];
            if (item.imageHighlighted != nil) {
                [button setImage:item.imageHighlighted forState:UIControlStateHighlighted];
            }
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            rect.origin.x += _unitWidth;
        }
    }
    return self;    
}

- (void)setCount:(NSInteger)count forItemAtIndex:(NSInteger)index{
    if (index < [self.items count]) {
        MSSegmentedButtonItem *item = [self.items objectAtIndex:index];
        item.count = count;
        UILabel *label = (UILabel *)[self viewWithTag:kMSSegmentedButtonLabelTag + index];
        label.text = [NSString stringWithFormat:@"%d",count];
    }
}

- (void)setImage:(UIImage *)image forItemAtIndex:(NSInteger)index{
    if (index < [self.items count]) {
        MSSegmentedButtonItem *item = [self.items objectAtIndex:index];
        item.image = image;
        UIButton *button = (UIButton *)[self viewWithTag:kMSSegmentedButtonButtonTag + index];
        [button setImage:image forState:UIControlStateNormal];
    }    
}

- (IBAction)buttonAction:(UIButton *)sender{
    if (_delegate && [_delegate respondsToSelector:@selector(segmentedButton:clickedButtonAtIndex:)]) {
        [_delegate segmentedButton:self clickedButtonAtIndex:sender.tag - kMSSegmentedButtonButtonTag];
    }
}

@end
